﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Kaixin.House.Garden
{
    using Kaixin.Tools;

    public class Seed
    {
        public string Name {get;set;}
        public string SeedId { get; set; }
        public string Price { get; set; }
    }


    public class SeedList
    {
        private Network _network;
        private string _verify;
        private static bool _updated = false;

        private static List<Seed> _Seeds = new List<Seed>();

        public static List<Seed> Seeds
        {
            get
            {
                return _Seeds;
            }
        }

        public SeedList(string verify, Network network)
        {
            _verify = verify;
            _network = network;
        }

        public void UpdateSeedList()
        {
            if (_updated)
                return;

            string url = Consts.RootURL + Consts.SeedListUrl;
            if (!_verify.Equals(""))
                url += "?verify=" + _verify;

            try
            {
                string resp = _network.CallUrl(url);
                XMLParser parser = new XMLParser(resp);
                XmlNodeList nodelist = parser.GetNodeList("/data/seed");

                _Seeds.Clear();
                for (int i = 0; i < nodelist.Count; i++)
                {
                    if (nodelist.Item(i).NodeType == XmlNodeType.Text)
                        continue;
                    _Seeds.Add(XmlNodeToSeed(nodelist.Item(i)));
                }
                _updated = true;
            }
            catch (Exception)
            { 
            }
        }

        private Seed XmlNodeToSeed(XmlNode node)
        {
            Seed seed = new Seed();

            XmlElement element = (XmlElement)node;
            seed.Name = element.GetElementsByTagName("name").Item(0).InnerText;
            seed.SeedId = element.GetElementsByTagName("seedid").Item(0).InnerText;
            seed.Price = element.GetElementsByTagName("price").Item(0).InnerText;

            return seed;
        }

        public static bool IsRareSeed(string seedid)
        {
            if (seedid.Equals(""))
                return true;
            else
                return false;
        }

        public static string FindSeedName(string seedid)
        {
            for (int i = 0; i < _Seeds.Count; i++)
                if (_Seeds[i].SeedId.Equals(seedid))
                    return _Seeds[i].Name;

            return "";
        }

        public static Seed GetSeedById(string seedid)
        {
            for (int i = 0; i < _Seeds.Count; i++)
                if (_Seeds[i].SeedId.Equals(seedid))
                    return _Seeds[i];

            return null;
        }
    }


    public class MySeed
    {
        public string Name;
        public string SeedId;
        public int Num;
        public int Type = 0; // 0: common, 1: love, 2: youyiguo

        public static string LoveFlower = "爱情花";
        public static string YouyiGuo = "友谊果";

        private static object _lock = new object();
        public string GetSeed()
        {
            lock (_lock)
            {
                if (Num > 0)
                {
                    Num--;
                    return SeedId;
                }
                else
                    return "";
            }
        }
    }

    public class MySeedList
    {
        private Network _network;

        public List<MySeed> _Seeds = new List<MySeed>();

        public List<MySeed> Seeds
        {
            get
            {
                return _Seeds;
            }
        }

        public MySeedList(Network network)
        {
            _network = network;
        }

        /// <summary>
        /// Get a random seedid in the list available
        /// Exclude love flower and youyiguo
        /// </summary>
        /// <returns></returns>
        public string GetRandomSeed() 
        {
            if (GetSeedNum() > 0)
            {
                Random r = new Random((int)DateTime.Now.Ticks);
                int i = 0;
                string seedid = "";

                do
                {
                    //i = (int)(r.NextDouble() * (_Seeds.Count - 1));
                    i = (int)(r.NextDouble() * _Seeds.Count);

                    if (_Seeds[i].Type == 0) // exclude love flower and youyiguo
                        seedid = _Seeds[i].GetSeed();
                } while (seedid.Equals(""));

                return seedid;
            }
            else
            {
                TextOutput.PrintLine("No Seeds !!!");
                return "";
            }
        }

        private int GetSeedNum()
        {
            int n = 0;

            for (int i=0;i<_Seeds.Count;i++)
                if (_Seeds[i].Type == 0)    // exclude love flower and youyiguo
                    n += _Seeds[i].Num;

            return n;
        }

        public void UpdateSeedList(string verify)
        {
            int page = 1;
            int totalpage = 1;

            _Seeds.Clear();
            totalpage = GetMySeedList(verify, page);
            if (totalpage > 1)
                for (int i = 2; i < totalpage + 1; i++)
                    GetMySeedList(verify, i);

        }

        private int GetMySeedList(string verify, int page)
        {
            string url = Consts.RootURL + Consts.MySeedListUrl;
            if (!verify.Equals(""))
                url += "?verify=" + verify;
            url += "&page=" + page;

            try
            {
                string resp = _network.CallUrl(url);
                XMLParser parser = new XMLParser(resp);
                XmlNodeList nodelist = parser.GetNodeList("/data/seed");

                string pages = parser.GetNodeValue("/data/totalpage");
                int totalpage = (pages == null || pages.Equals("")) ? 0 : Convert.ToInt32(pages);

                for (int i = 0; i < nodelist.Count; i++)
                {
                    if (nodelist.Item(i).NodeType == XmlNodeType.Text)
                        continue;
                    _Seeds.Add(XmlNodeToMySeed(nodelist.Item(i)));
                }

                return totalpage;
            }
            catch (Exception)
            {
                return -1;
            }
        }

        private MySeed XmlNodeToMySeed(XmlNode node)
        {
            MySeed seed = new MySeed();

            XmlElement element = (XmlElement)node;
            seed.Name = element.GetElementsByTagName("name").Item(0).InnerText;
            seed.SeedId = element.GetElementsByTagName("seedid").Item(0).InnerText;
            seed.Num = Convert.ToInt32(element.GetElementsByTagName("num").Item(0).InnerText);

            if (seed.Name.Equals(MySeed.LoveFlower))
                seed.Type = 1;
            else if (seed.Name.Equals(MySeed.YouyiGuo))
                seed.Type = 2;

            return seed;
        }
    }
}
